package com.bdc.user.action;

import com.bdc.user.dao.IUserDao;
import com.bdc.user.domain.UserInfo;
import com.bdc.user.service.IUserService;
import com.bdc.utils.StringUtils;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import com.opensymphony.xwork2.Action;
import org.apache.log4j.Logger;

/**
 * User: shawn
 * Date: 2011-8-28
 * Time: 22:42:43
 */
public class ChangePasswordAction {

    private Logger logger = Logger.getLogger(RegisterAction.class.getName());

    @Inject
    private IUserDao userDao;
    @Inject
    private IUserService userService;

    private String mail;
    private String oldPassword;
    private String newPassword;

    @Transactional
    public String changePassword() throws Exception {
        logger.debug("change password action start");

        UserInfo userInfo = userDao.getUserInfo(mail);
        if (userInfo == null) {
            return Action.ERROR;
        }

        boolean isWrongPassword = !StringUtils.SHAString(oldPassword).equals(userInfo.getPassword());
        if (isWrongPassword) {
            return Action.ERROR;
        }

        newPassword = StringUtils.SHAString(newPassword);
        userInfo.setPassword(newPassword);

        return Action.SUCCESS;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }
}
